/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.cvut.fel.taskpro.view.controller;

import cz.cvut.fel.taskpro.data.entity.Admin;
import cz.cvut.fel.taskpro.data.entity.Kantor;
import cz.cvut.fel.taskpro.data.entity.Student;
import cz.cvut.fel.taskpro.data.entity.TaskProUser;
import java.security.Principal;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;

/**
 * Abstract controller which implements recognize user action and print message
 * service.
 * @author Tom
 * @version 1.0.0. beta
 */
public abstract class AbstractController {
    @ManagedProperty(value="#{loginBean}")
    protected LoginBean loginBean;
    protected Kantor loggedKantor;
    protected Admin loggedAdmin;
    protected Student loggedStudent;

    public LoginBean getLoginBean() {
        return loginBean;
    }

    public void setLoginBean(LoginBean loginBean) {
        this.loginBean = loginBean;
    }

    /**
     * This method get actual logged principal from JAAS context.
     * @return actual logged user who is logged in.
     */
    protected Principal getPrincipal() {
        return FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
    }
    /**
     * This method recognize if logged user is Kantor, Student, admin
     */
    public void recognizeLogin(){
        TaskProUser user=loginBean.getLoggedUser(); 
        if(user instanceof Admin){
            loggedAdmin=(Admin) user;
        }
        else if(user instanceof Kantor){
            loggedKantor=(Kantor) user;
        }
        else if(user instanceof  Student){
         loggedStudent=(Student) user;   
        }
        
    }
    /**
     * This method print error type message to faces context.
     * @param code code of error
     * @param message message of error
     */
    protected void printErrMessage(int code, String message){
        FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, Integer.toString(code), message));
    }
    /**
     * This method print erro type message to faces context
     * @param content headlight of error message 
     * @param message message of error
     */
      protected void printErrMessage(String content, String message){
        FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, content, message));
    }
    /**
     * This method print successfull type message to facase context
     * @param content headlight of successfull message
     * @param message message of successfull message
     */
    protected void printSuccMessage(String content,String message){
     FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_INFO, content, message));
    }
    /**
     * This method print warning type message to facase context
     * @param content headlight of warning message
     * @param message message of warning message
     */
    protected void printWarning(String content,String message){
         FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_WARN, content, message));
    }
}
